<html>
<head>
    <title>商品维护页面</title>
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/layui/layui.js"></script>
    <script src="/res/js/tools.js"></script>
</head>
<body>
<form class="layui-form" action="" id="myform" lay-filter="example">
    <input type="hidden" name="pdId">
    <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
            <input type="text" id="pdName" name="pdName" lay-verify="required|pdName" autocomplete="off" placeholder="请输入商品名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-block">
            <input type="text" id="pdPrice" name="pdPrice" lay-verify="required|number" autocomplete="off" placeholder="请输入价格" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品图片</label>
        <div class="layui-input-block">
            <input type="file" name="fileImg" id="pdPhoto">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">商品描述</label>
        <div class="layui-input-block">
            <input type="text" id="pdRemark" name="pdRemark" lay-verify="required|pdRemark" autocomplete="off" placeholder="商品介绍" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll()">关闭</button>
        </div>
    </div>
</form>
<script>
    layui.use(['table', 'laydate'], function() {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate;
        var $ = layui.$;
        //日期
        laydate.render({
            elem: '#stuBirthday'
        });

        //监听提交
        form.on('submit(demo1)', function (data) {
            let formData = new FormData(document.getElementById("myform"));
            let url = "/product/addOrUpdate";
            $.ajax({
                async: true, //true异步.  false 同步
                type: "POST", //请求方式
                url: url,    //请求的路径
                data: formData,  //请求的参数
                contentType: false, //发送信息至服务器时内容编码类型
                processData: false, //通过data选项传递进来的数据
                dataType: "json", //返回类型
                success: function (resp) { //成功回调函数
                    if (resp.state == 1) {
                        //在子页面里面调用父页面的方法
                        parent.closeLayer(resp.msg);
                    } else {
                        layer.msg(resp.msg);
                    }
                }, error: function () { //失败的回调函数
                }
            });
            return false;
        });

        //给表单赋值
        setFormValue();
        function setFormValue() {
            let pdId = getQueryString("id");
            if (pdId) { //pdId==""  pdId==null  pdId==undefined
                let url_ = "/product/selectById";
                let data_ = {id: pdId};
                $.post(url_, data_, function (resp) {
                    console.info(resp);
                    if (resp.state == 1) {
                        //给表单赋值
                        form.val('example', resp.msg);
                        console.info("赋值的内容是：",resp.msg);
                    }
                }, "json")
            }
        }
    });
</script>
</body>
</html>
